package com.onegene.pdf.component.event;

import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.onegene.pdf.component.Constant;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author laoliangliang
 * @since 2020-05-24 13:53
 */
public class CatalogMoveEvent implements IEventHandler {

    private int pageSize = 0;
    private Properties properties;
    @Getter
    private List<PdfPage> pagesMove = new ArrayList<>();

    public CatalogMoveEvent(Properties properties) {
        this.properties = properties;
    }

    public int getPageSize() {
        return pageSize;
    }

    @Override
    public void handleEvent(Event event) {
        pageSize++;
        properties.setProperty(Constant.CATALOG_SIZE, pageSize + "");

        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfDocument pdfDoc = docEvent.getDocument();
        int pageNumber = pdfDoc.getNumberOfPages();
        PdfPage page = pdfDoc.getPage(pageNumber);
        pagesMove.add(page);
//        pdfDoc.movePage(pageNumber, 5);
    }
}
